﻿@model Class
@{
    ViewData["Title"] = $"Group g{Model.Id}";
    IPagedList<Student> studs = ViewBag.Students;
    var prefix = Model.AffiliationId + "_";
    int uid = int.Parse(User.GetUserId());
}

<h2 class="mt-2 mb-3">@ViewData["Title"]</h2>

<div class="row">
    <div class="col-lg-4">
        <table class="table table-sm table-striped">
            <tr><th>ID</th><td>@Model.Id</td></tr>
            <tr><th>Name</th><td>@Model.Name</td></tr>
            <tr><th>Count</th><td>@studs.TotalCount</td></tr>
            <tr><th>Creation Time</th><td>@Html.CstTime(Model.CreationTime)</td></tr>
            <tr><th>Creator</th><td>@(Model.UserName ?? "N/A")</td></tr>
            <tr><th>Shared</th><td>@(Model.UserId.HasValue ? "No" : "Yes")</td></tr>
        </table>
    </div>
</div>

<p>
    <a asp-action="Clone" class="btn btn-info" data-toggle="ajaxWindow" data-target="cloneClass" title="clone class"><i class="fas fa-clone"></i>&nbsp;Clone</a>
    <a asp-show-if="@(Model.UserId == null || Model.UserId == uid)" asp-action="Delete" class="btn btn-danger" data-toggle="ajaxWindow" data-target="deleteClass" title="delete class"><i class="fas fa-trash-alt"></i>&nbsp;Delete</a>
</p>

<h3 class="mt-3">Students in g@(Model.Id)</h3>

<div class="d-none" id="paging">
    <pagination bs-current-page="studs.CurrentPage" bs-total-page="studs.TotalPage" />
</div>

<div class="table-wrapper">
    <table class="data-table table table-sm table-striped" style="width:auto">
        <thead>
            <tr role="row">
                <th scope="col" class="sortable searchable" style="min-width:3em">ID</th>
                <th scope="col" class="sortable searchable" style="min-width:4em">name</th>
                <th scope="col" class="sortable searchable" style="min-width:10em">user</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            @foreach (var stu in studs)
            {
                var stuid = stu.Id.StartsWith(prefix) ? stu.Id.Substring(prefix.Length) : stu.Id;
                <tr asp-controller="Users" asp-action="Detail" asp-route-uid="@stu.UserId" role="row" data-toggle="gotoUrl">
                    <td>@stuid</td>
                    <td>@stu.Name</td>
                    <td class="@(stu.IsVerified.GetValueOrDefault() ? "" : "ignore")" use-a>@stu.UserName</td>
                    <td><a asp-show-if="@(Model.UserId == null || Model.UserId == uid)" asp-action="Kick" asp-route-stuid="@stuid" title="kick this student from this group" data-toggle="ajaxWindow" data-target="deleteStud"><i class="fas fa-trash-alt"></i></a></td>
                </tr>
            }
        </tbody>
    </table>
</div>

<script>
    $().ready(function () {
        $('.data-table').DataTable({
            "paging": false,
            "searching": true,
            "ordering": true,
            "order": [[0, 'asc']],
            "info": false,
            "autoWidth": true,
            "language": {
                "searchPlaceholder": "filter table",
                "search": "_INPUT_",
            },
            'aoColumnDefs': [
                { aTargets: ['sortable'], bSortable: true },
                { aTargets: ['searchable'], bSearchable: true },
                { aTargets: ['_all'], bSortable: false, bSearchable: false }
            ],
        });

        $('.dataTables_wrapper .row:first-child .col-md-6:first-child').html($('#paging').html());
        $('.dataTables_wrapper .pagination-sm').addClass('mb-0');
    });
</script>

<p asp-show-if="@(Model.UserId == null || Model.UserId == uid)">
    <a asp-action="Add" class="btn btn-primary" data-toggle="ajaxWindow" data-target="addStud" title="add students"><i class="fas fa-plus"></i>&nbsp;Add</a>
</p>
